 package com.zu.commu.auto1_0.protocol.call24Hour;
 
 import com.zu.commu.auto1_0.DataType1_0;
import com.zu.commu.auto1_0.protocol.CheckData;
import com.zu.commu.auto1_0.protocol.Util;
import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.Data;

public class Call24DataProtocol
{
  public Data[] analyseReportData(byte[] b)
    throws Exception
  {
    new CheckData().checkCrc(b, 4, 395, 396);
    Data[] d = (Data[])null;
    d = new Data[24];
    int n = 0;
    for (int i = 0; i < 24; i++) {
      n = i * 16;
      d[i] = new Data();
      getMeterNo(b, d[i]);
      getDateTime(b, d[i], n);
      getRemainWater(b, d[i], n);
      getAccumulateFlux(b, d[i], n);
      getInstantFlux(b, d[i], n);
      getWaterLevel(b, d[i], n);
      d[i].setDataType(DataType1_0.REPORT24DATA.toInt()+"");
    }
    return d;
  }

  private void getMeterNo(byte[] b, Data d)
    throws Exception
  {
    try
    {
      Util u = new Util();
      d.setMeterNo(u.byte2Hex(b, 4, 9));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取测控器地址号时出错！" + e.getMessage(), null);
    }
  }

  private void getDateTime(byte[] b, Data d, int n)
    throws Exception
  {
    try
    {
      Util u = new Util();
      d.setDateTime(u.byte2Hex(b, 12 + n, 15 + n));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取时间时出错！" + e.getMessage() + n, e);
    }
  }

  private void getRemainWater(byte[] b, Data d, int n)
    throws Exception
  {
    try
    {
      Util u = new Util();
      double dd = u.string2Double(u.clean0FromStrNum(u.byte2Hex(b, 16 + n, 19 + n)));
      if (dd > 10000000.0D) {
        dd = 10000000.0D - dd;
      }
      d.setRemainWater(Double.valueOf(dd));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取剩余水量时出错！" + e.getMessage(), null);
    }
  }

  private void getAccumulateFlux(byte[] b, Data d, int n)
    throws Exception
  {
    try
    {
      Util u = new Util();
      double dd = u.string2Double(u.clean0FromStrNum(u.byte2Hex(b, 20 + n, 23 + n)));
      if (dd > 10000000.0D) {
        dd = 10000000.0D - dd;
      }
      d.setAccumulateFlux(Double.valueOf(dd));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取累积用水量时出错！" + e.getMessage(), null);
    }
  }

  private void getInstantFlux(byte[] b, Data d, int n)
    throws Exception
  {
    try
    {
      Util u = new Util();
      d.setInstantFlux(Double.valueOf(u.string2Double(u.clean0FromStrNum(u.byte2Hex(b, 24 + n, 25 + n)))));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取瞬时流量时出错！" + e.getMessage(), null);
    }
  }

  private void getWaterLevel(byte[] b, Data d, int n)
    throws Exception
  {
    try
    {
      Util u = new Util();
      String hight = u.clean0FromStrNum(u.byte2Hex(b, 26 + n, 26 + n));
      String low = u.clean0FromStrNum(u.byte2Hex(b, 27 + n, 27 + n));
      d.setWaterLevel(Double.valueOf(u.string2Double(hight + "." + low)));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取水位时出错！" + e.getMessage(), null);
    }
  }
}




